package routines;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

@Entity
@Table(name = "customerinfo")
public class CustomerInfo extends Print {

	private Long id;
	private LegalEntity legalentity;
	private String regNumber;

	private String okpo;
	private String okved;
	private List<Budget> budgets=null;
	
	public CustomerInfo AddBudget(Budget b){
		if (b.getCode()==null)
			return this;
		if(budgets==null)
			budgets = new ArrayList<Budget>();
		if (findBudget(b.getCode())==null){
			b.setCustomerinfo(this);
			budgets.add(b);
		}
		return this;
	}
	
	/**
	 * Поиск бюджета по его коду
	 * @param code кода бюджета
	 * @return Budget, если найден, иначе null
	 */
	private Budget findBudget(String code){
		if(budgets==null)
			return null;
		for (Budget b: budgets){
			if (code.equals(b.getCode()))
				return b;
		}
		return null;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "seq_customerinfo_gen")
	@SequenceGenerator(name = "seq_customerinfo_gen", sequenceName = "seq_customerinfo")
	@Column(name = "customerinfo_id")
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "legalentity_id", nullable = false)
	@Fetch(FetchMode.JOIN)
	public LegalEntity getLegalentity() {
		return legalentity;
	}

	public void setLegalentity(LegalEntity legalentity) {
		this.legalentity = legalentity;
	}

	@Column(name = "regnumber", columnDefinition = "TEXT")
	public String getRegNumber() {
		return regNumber;
	}

	public CustomerInfo setRegNumber(String regNumber) {
		this.regNumber = regNumber;
		return this;
	}

	@Column(name = "okpo", columnDefinition = "TEXT")
	public String getOkpo() {
		return okpo;
	}

	public CustomerInfo setOkpo(String okpo) {
		this.okpo = okpo;
		return this;
	}

	@Column(name = "okved", columnDefinition = "TEXT")
	public String getOkved() {
		return okved;
	}

	public CustomerInfo setOkved(String okved) {
		this.okved = okved;
		return this;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "customerinfo")
	@Cascade({ CascadeType.SAVE_UPDATE })
	@OnDelete(action = OnDeleteAction.CASCADE)
	public List<Budget> getBudgets() {
		return budgets;
	}

	public void setBudgets(List<Budget> budgets) {
		this.budgets = budgets;
	}

}
